package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Menu;
import com.ruoyi.system.domain.res.doseMenuDetail.DayMenu_res;
import com.ruoyi.system.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DoseMenuDetailMapper;
import com.ruoyi.system.domain.DoseMenuDetail;
import com.ruoyi.system.service.IDoseMenuDetailService;

import javax.annotation.Resource;

/**
 * 每周菜单详情Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-20
 */
@Service
public class DoseMenuDetailServiceImpl implements IDoseMenuDetailService {
    @Resource
    private DoseMenuDetailMapper doseMenuDetailMapper;

    @Resource
    private MenuMapper menuMapper;

    /**
     * 查询每周菜单详情
     *
     * @param id 每周菜单详情主键
     * @return 每周菜单详情
     */
    @Override
    public DoseMenuDetail selectDoseMenuDetailById(Long id) {
        return doseMenuDetailMapper.selectDoseMenuDetailById(id);
    }

    /**
     * 查询每周菜单详情列表
     *
     * @param doseMenuDetail 每周菜单详情
     * @return 每周菜单详情
     */
    @Override
    public List<DoseMenuDetail> selectDoseMenuDetailList(DoseMenuDetail doseMenuDetail) {
        return doseMenuDetailMapper.selectDoseMenuDetailList(doseMenuDetail);
    }

    /**
     * 查询每日菜谱
     */
    @Override
    public List<DayMenu_res> selectDayMenu(DoseMenuDetail doseMenuDetail) {
        List<DayMenu_res> resList = new ArrayList<>();
        List<DoseMenuDetail> list = doseMenuDetailMapper.selectDoseMenuDetailList_day(doseMenuDetail);
        list.stream().collect(Collectors.groupingBy(DoseMenuDetail::getMenuId)).forEach((k, v) -> {
            DayMenu_res res = new DayMenu_res();
            Menu menu = menuMapper.selectMenuById(k);
            if (menu != null) {
                res.setMenuName(menu.getName());
            }
            if (!v.isEmpty()) {
                res.setTime(v.get(0).getTime());
            }
            resList.add(res);
        });
        resList.sort(Comparator.comparing(DayMenu_res::getTime));
        return resList;
    }

    /**
     * 新增每周菜单详情
     *
     * @param doseMenuDetail 每周菜单详情
     * @return 结果
     */
    @Override
    public int insertDoseMenuDetail(DoseMenuDetail doseMenuDetail) {
        doseMenuDetail.setCreateTime(DateUtils.getNowDate());
        return doseMenuDetailMapper.insertDoseMenuDetail(doseMenuDetail);
    }

    /**
     * 修改每周菜单详情
     *
     * @param doseMenuDetail 每周菜单详情
     * @return 结果
     */
    @Override
    public int updateDoseMenuDetail(DoseMenuDetail doseMenuDetail) {
        doseMenuDetail.setUpdateTime(DateUtils.getNowDate());
        return doseMenuDetailMapper.updateDoseMenuDetail(doseMenuDetail);
    }

    /**
     * 批量删除每周菜单详情
     *
     * @param ids 需要删除的每周菜单详情主键
     * @return 结果
     */
    @Override
    public int deleteDoseMenuDetailByIds(Long[] ids) {
        return doseMenuDetailMapper.deleteDoseMenuDetailByIds(ids);
    }

    /**
     * 删除每周菜单详情信息
     *
     * @param id 每周菜单详情主键
     * @return 结果
     */
    @Override
    public int deleteDoseMenuDetailById(Long id) {
        return doseMenuDetailMapper.deleteDoseMenuDetailById(id);
    }
}
